package com.briup.web.servlet;

import com.briup.bean.*;
import com.briup.service.IOrderFormService;
import com.briup.service.IOrderLineService;
import com.briup.service.IShopAddressService;
import com.briup.service.impl.OrderFormServiceImpl;
import com.briup.service.impl.OrderLineServiceImpl;
import com.briup.service.impl.ShopAddressServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@WebServlet(name = "OrderCommitServlet", value = "/orderCommit")
public class OrderCommitServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        IShopAddressService shopAddressService = new ShopAddressServiceImpl();
        IOrderFormService orderFormService = new OrderFormServiceImpl();
        IOrderLineService orderLineService = new OrderLineServiceImpl();

        //1、接收参数
        String receiveName = request.getParameter("receiveName");
        //接收其他参数...
        String address = request.getParameter("address");
        String phone = request.getParameter("phone");
        String shipAddId = request.getParameter("shipAddId");

        //获取当前登录用户
        HttpSession session = request.getSession();
        Customer customer = (Customer) session.getAttribute("customer");

        //判断一下地址是新增还是原来的地址
        //如果说前端获取到的receiveName，address，phone不为空的话，就认为要新增地址
        //判断receiveName是否为空，还要判断address，phone
        ShopAddress shopAddress = new ShopAddress();
        if (receiveName.equals("") && address.equals("") && phone.equals("")) {
            //说明使用旧的地址
            //没有选择地址的情况下
            if (shipAddId.equals("")) {
                response.sendRedirect("/confirm.jsp");
                return;
            } else {
                Integer addId = Integer.valueOf(shipAddId);
                shopAddress.setId(addId);
            }
        } else {
            //输入框不为空，设置为新地址
            shopAddress.setAddress(address);
            shopAddress.setReceiveName(receiveName);
            shopAddress.setPhone(phone);
            shopAddress.setCustomer(customer);
            //保存新地址saveAddress，地址id自动生成
            shopAddressService.saveAddress(shopAddress);
        }
        //可以通过接收参数的形式获取旧地址的id
        //通过这个地址id查询地址信息（拿到id之后new ShopAddress只把id放进去）
        //开始准备一个OrderFrom对象（时间可以直接new Date）

        //获取购物车信息
        List<ShopCar> shopCarList = (List<ShopCar>) session.getAttribute("confirm");
        //遍历购物车记录，求订单总价
        Double total = 0.0;
        for (ShopCar shopCar : shopCarList) {
            total += shopCar.getBook().getPrice() * shopCar.getNum();
        }

        //订单信息
        OrderForm orderForm = new OrderForm();
        orderForm.setCost(total);
        orderForm.setOrderdate(new Date());
        orderForm.setShopAddress(shopAddress);
        orderForm.setCustomer(customer);
        //并且调用对应的方法saveOrderFrom（需要selectKey）
        orderFormService.saveOrderForm(orderForm);//保存订单信息

        //把他对应的订单项存入数据库当中（需要订单表的id）
        for (ShopCar shopCar : shopCarList) {
            //订单
            OrderLine orderLine = new OrderLine();
            orderLine.setBook(shopCar.getBook());
            orderLine.setNum((long) shopCar.getNum());
            orderLine.setCost(shopCar.getBook().getPrice() * shopCar.getNum());
            orderLine.setOrderForm(orderForm);
            //保存订单项
            orderLineService.saveOrderLine(orderLine);
        }
        //结束
        response.sendRedirect("/orderList");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
